package com.xianbeiying.app.ckgl.activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.DeviceFragmentAdapter;
import com.xianbeiying.app.ckgl.bean.DevicesBean;
import com.xianbeiying.app.ckgl.bean.FaMenBean;
import com.xianbeiying.app.ckgl.bean.QueZQBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.data.DataUtil;
import com.xianbeiying.app.ckgl.data.EQP;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.util.LogUtils;
import com.xianbeiying.app.ckgl.widght.dapengView.DpView;
import com.xianbeiying.app.ckgl.widght.dapengView.Dpdata;

import java.util.ArrayList;
import java.util.List;

/***
 * 实时状态
 */
public class RealTimeModeActivity extends BaseActivity implements View.OnClickListener,HttpCallBackUtil,Dpdata{
//    private NoScrollListView rtime_mode_list;
    //水平滑动的
    private LinearLayout ly_rtmd_horizontal;
    private ImageView iv_title_left;
    private TextView tv_title;
    private View va_line;
    private TextView tv_va_all;
    private GetModel getModel;
//    private ProgressBar progressBar;
//    private TextView tv_wcy;//完成率;
    private ListView device_list;
    private DeviceFragmentAdapter deviceFragmentAdapter;
    private FaMenBean faMenBean;
    private ArrayList<FaMenBean.VBean.STATUSBean> listFa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_real_time_mode);
        initView();
    }
    private void initView(){
//        rtime_mode_list = (NoScrollListView) findViewById(R.id.rtime_mode_list);
        va_line = (View) findViewById(R.id.va_line);
//        progressBar = (ProgressBar) findViewById(R.id.progressBar);
//        tv_wcy = (TextView) findViewById(R.id.tv_wcy);
        tv_va_all = (TextView) findViewById(R.id.tv_va_all);
        ly_rtmd_horizontal = (LinearLayout) findViewById(R.id.ly_rtmd_horizontal);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        device_list=(ListView)findViewById(R.id.device_list);

        getModel=new GetModel(this);
        getModel.addResponseListener(this);
        initData();
        initEvent();
    }

    private void initData() {
        tv_title.setText("实时状态");
        if(MyApplication.devicesBean != null) {
            devicesBean = MyApplication.devicesBean;
            getData(devicesBean.getGREENHOUSE(),devicesBean.getGREENHOUSE().size(), 0);
            getModel.QUWEYDEVSDetauls("GET_ALLDEVICES_STATUS", devicesBean.getGREENHOUSE().get(0).getGreenhouseId(), true);
        }else{
            //获取大棚数据
            getModel.QUWEYDEVS(true);
        }
    }

    private void initEvent() {
        iv_title_left.setOnClickListener(this);
        tv_va_all.setOnClickListener(this);
    }
    private DpView dpView;
    private void getData(List<DevicesBean.GREENHOUSEBean> DEVICES,int size, final int id) {
        dpView = new DpView(DEVICES, size,id, ly_rtmd_horizontal, this);
        dpView.setDpdata();
        dpView.addResponseListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.tv_va_all:
                tv_va_all.setVisibility(View.GONE);
                va_line.setVisibility(View.GONE);
                break;
        }
    }
    //网络返回值
    private DevicesBean devicesBean;
    private QueZQBean queZQBean;//查询周期计划获取的参数信息
    @Override
    public void onCallBackData(String url, Object content) {
        if (url.equals(UrlBean.QUWEYDEVS)) {
            if (content != null) {
                devicesBean = JSON.parseObject(new String((byte[])content), DevicesBean.class);
                MyApplication.devicesBean = devicesBean;
                getData(devicesBean.getGREENHOUSE(),devicesBean.getGREENHOUSE().size(), 0);
                getModel.QUWEYDEVSDetauls("GET_ALLDEVICES_STATUS", devicesBean.getGREENHOUSE().get(0).getGreenhouseId(), true);
            }
        }
        if(url.equals("大棚信息")){
            if (content != null) {
                faMenBean = JSON.parseObject(new String((byte[]) content), FaMenBean.class);
                if (faMenBean.isR()) {
                    listFa = DataUtil.getSeBei(faMenBean, EQP.EQP_TYPE_EMV);
                    listFa.addAll(DataUtil.getSeBei(faMenBean, EQP.EQP_TYPE_FAN));
                    listFa.addAll(DataUtil.getSeBei(faMenBean, EQP.EQP_TYPE_HEALER));
                    deviceFragmentAdapter = new DeviceFragmentAdapter(this,listFa);
                    device_list.setAdapter(deviceFragmentAdapter);
                }
//            queZQBean = JSON.parseObject(new String((byte[]) content), QueZQBean.class);
//                if (queZQBean.isR()) {
//                    if (queZQBean.getV().getPLANS()!=null&&queZQBean.getV().getPLANS().size()!=0&&queZQBean.getV().getPLANS().get(0).getSTATUS() == 1) {
//                        ArrayList<String> endList = DateStatusManager.getInstance().getEndList(devicesBean, queZQBean);
//                        ArrayList<String> runList = DateStatusManager.getInstance().getPlantList(devicesBean, queZQBean);
//                        ArrayList<String> nextList = DateStatusManager.getInstance().getNextList(devicesBean, queZQBean);
//                        rtime_mode_list.setAdapter(new ReTimeModeAdapterss(this, endList, runList, nextList));
//                        if (endList.size() != 0) {
//                            if (runList.size() != 0 && nextList.size() != 0) {
//                                progressBar.setProgress(100 / 3);
//                                tv_wcy.setText("完成率" + (100 / 3) + "%");
//                            } else {
//                                if (runList.size() == 0 && nextList.size() == 0) {
//                                    progressBar.setProgress(100);
//                                    tv_wcy.setText("完成率 100%");
//                                } else {
//                                    progressBar.setProgress(100 / 2);
//                                    tv_wcy.setText("完成率 50%");
//                                }
//
//                            }
//                        } else {
//                            progressBar.setProgress(0);
//                            tv_wcy.setText("完成率 0%");
//                        }
//
//                    }else{
//                        rtime_mode_list.setAdapter(null);
//                    }
//
////                    JSONObject v = response.getJSONObject("V");
////                    List<PlanDetailForView> resDetails = PlanUtil.getPlan(v, Cmd.TASK_TYPE_CYCLE_TAP);
////                    for (PlanDetailForView detail : resDetails)
////                    {
////                        if (detail.getStatus() == 1)
////                        {
////                            DateStatusManager.getInstance().putDateStatus(DateUtil.strToDateLong(detail.getBeginTime()), DateUtil.strToDateLong(detail.getEndTime()),
////                                    Long.parseLong(detail.getDuration()), Long.parseLong(detail.getPeroid()), String.valueOf(detail.getFarmId() + 1));
////                        }
////                    }
////                    setStatusValue();
                }
        }
    }
    private int faId;
    @Override
    public void onDpdata(int sid, boolean isChoice) {
        LogUtils.e("00",sid+"====");
        if (isChoice) {
            faId = sid;
            getModel.QUWEYDEVSDetauls("GET_ALLDEVICES_STATUS", devicesBean.getGREENHOUSE().get(0).getGreenhouseId(), true);
        }
    }
}
